package xmlStateMachine {

	import de.ghost23.common.statemachine.StateMachine;
	import flash.net.URLLoader;
	import xmlStateMachine.commands.ParseXML;
	import xmlStateMachine.commands.RequestFile;
	import xmlStateMachine.commands.Reset;
	import xmlStateMachine.commands.UnloadFile;

	public class XMLStateMachine extends StateMachine {

		// Possible incoming events to change state
		public static const FILE_LOAD_REQUESTED:String      = "onFileLoadRequested";
		public static const FILE_LOAD_COMPLETE:String       = "onFileLoadComplete";
		public static const FILE_LOAD_FAILED:String         = "onFileLoadFailed";
		public static const FILE_UNLOAD_REQUESTED:String    = "onFileUnloadRequested";
		public static const XML_PARSING_STARTED:String      = "onXMLParsingStarted";
		public static const XML_PARSING_COMPLETE:String     = "onXMLParsingComplete";
		public static const XML_PARSING_FAILED:String       = "onXMLParsingFailed";
		public static const MACHINE_RESET:String            = "onMachineReset";

		// Possible data field names
		public static const PARSED_OBJECT:String            = "parsedObject";
		public static const FILE_URL:String                 = "fileURL";
		public static const FILE_RAW_XML:String             = "fileXML";


		// Data fields of this particular state machine

		public function injectNewFileUrl(url:String):void {
			data[FILE_URL] = url;
			try {
				setInput(FILE_LOAD_REQUESTED);
			}catch(e:Error) {
				trace("invoking event FILE_LOAD_REQUESTED not " +
						"possible in current state: " + currentState.identifier);
			}
		}

		public function get fileURL():String {
			return data[FILE_URL];
		}

		public function get rawXMLFile():URLLoader {
			return data[FILE_RAW_XML];
		}

		public function get parsedXMLObject():Object {
			return data[PARSED_OBJECT];
		}

		// Definition of the state machine
		public function XMLStateMachine() {

			super(XMLStates.EMPTY_IDLE);

			states = XMLStates;

			XMLStates.EMPTY_IDLE.inputCommand = Reset;
			XMLStates.EMPTY_IDLE.stateAfterEvent[XMLStates.FILE_REQUESTING] = FILE_LOAD_REQUESTED;

			XMLStates.FILE_REQUESTING.inputCommand = RequestFile;
			XMLStates.FILE_REQUESTING.stateAfterEvent[XMLStates.FILE_LOADED] = FILE_LOAD_COMPLETE;
			XMLStates.FILE_REQUESTING.stateAfterEvent[XMLStates.FILE_LOADERROR] = FILE_LOAD_FAILED;

			XMLStates.FILE_LOADED.transient = true;
			XMLStates.FILE_LOADED.stateAfterEvent[XMLStates.XML_PARSING] = XML_PARSING_STARTED;

			XMLStates.FILE_LOADERROR.transient = true;
			XMLStates.FILE_LOADERROR.stateAfterEvent[XMLStates.EMPTY_IDLE] = MACHINE_RESET;

			XMLStates.XML_PARSING.inputCommand = ParseXML;
			XMLStates.XML_PARSING.stateAfterEvent[XMLStates.XML_PARSED] = XML_PARSING_COMPLETE;
			XMLStates.XML_PARSING.stateAfterEvent[XMLStates.XML_REJECTED] = XML_PARSING_FAILED;

			XMLStates.XML_PARSED.transient = true;
			XMLStates.XML_PARSED.stateAfterEvent[XMLStates.FILE_UNLOADING] = FILE_UNLOAD_REQUESTED;

			XMLStates.XML_REJECTED.transient = true;
			XMLStates.XML_REJECTED.stateAfterEvent[XMLStates.FILE_UNLOADING] = FILE_UNLOAD_REQUESTED;

			XMLStates.FILE_UNLOADING.inputCommand = UnloadFile;
			XMLStates.FILE_UNLOADING.stateAfterEvent[XMLStates.EMPTY_IDLE] = MACHINE_RESET;
		}
	}
}